package com.frogs.ucenter.util.sms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.frogs.ucenter.entity.SmsMessageMqEntity;

@Component
public class PartnerSmsCFHelper {
	
	private static Logger log = LoggerFactory.getLogger(PartnerSmsCFHelper.class);
	private String sendUrl;
	private String sname;
	private String spwd;
	private String sprdid;
	private String scorpid;
	
	public void setSendUrl(String sendUrl) {
		this.sendUrl = sendUrl;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public void setSpwd(String spwd) {
		this.spwd = spwd;
	}

	public void setSprdid(String sprdid) {
		this.sprdid = sprdid;
	}

	public void setScorpid(String scorpid) {
		this.scorpid = scorpid;
	}

	private String SMS(String postData, String postUrl) {
		 try {
	        //发送POST请求
	        URL url = new URL(postUrl);
	        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	        conn.setRequestMethod("POST");
	        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	        conn.setRequestProperty("Connection", "Keep-Alive");
	        conn.setUseCaches(false);
	        conn.setDoOutput(true);
	
	        conn.setRequestProperty("Content-Length", "" + postData.length());
	        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
	        out.write(postData);
	        out.flush();
	        out.close();
	
	        //获取响应状态
	        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
	        	log.error("func[SMS] code[" + conn.getResponseCode() + "] desc[fail]");
	            return "";
	        }
	        //获取响应内容体
	        String line, result = "";
	        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
	        while ((line = in.readLine()) != null) {
	            result += line + "\n";
	        }
	        in.close();
	        return result;
	    } catch (IOException e) {
	    	log.error("func[SMS] postUrl[" + postUrl + "] postData["+postData+"] e["+e.getMessage()+"] desc[fail]");
	    }
	    return "";
	 }
	 
	 public String sendSms(SmsMessageMqEntity smsMessageMqEntity) {
		 String result = "";
		 try {
			 String PostData = "sname="+this.sname+"&spwd="+this.spwd+"&scorpid="+this.scorpid+""
			 		+ "&sprdid="+this.sprdid+"&sdst="+smsMessageMqEntity.getReceiver()+""
			 				+ "&smsg="+URLEncoder.encode(smsMessageMqEntity.getContent(),"utf-8");
			 result = this.SMS(PostData, this.sendUrl);
			 log.info("func[sendSms] phone[" + smsMessageMqEntity.getReceiver() + "] result[" + result + "] desc[get send result]");
		} catch (Exception e) {
			log.error("func[sendSms] result["+result+"] receiver[" + smsMessageMqEntity.getReceiver() + "]"
					+ " content["+smsMessageMqEntity.getContent()+"] error[" + e.getMessage() + "]", e);
		}
		 return result;
	 }
}
